window._MCA = {

    _JUMPPER:0.3,//传送出口概率

    //特殊情况下技能选择的优先级
    //1.同时有分身和自爆 优先分身
    //2.有自身状态技能且自身没有状态 优先状态技能 反之过滤状态技能
    speSkillUse:function(skillArr){

        var fenshenskill = null;
        var zibaoskill = null;

        for(var i=0;i<skillArr.length;i++){
            var skillname = skillArr[i];
            if(_SKILL[skillname]){
                var tempskillConf = _SKILL[skillname];
                if(_MCA.isFenshen(tempskillConf)){
                    fenshenskill = skillname;
                }else if(_MCA.isZibao(tempskillConf)){
                    zibaoskill = skillname;
                }else if(_MCA.isBuff(tempskillConf)){
                    if(
                        tempskillConf.status
                        &&!_STATUSHELPERCHESS.hasStatus(_CHESSFIGHT.currentPlayer,tempskillConf.status)
                    ){
                        return skillname;
                    }
                }
                /*else if(_MCA.isBuff(tempskillConf)){

                }else if(_MCA.isMove(tempskillConf)){

                }*/

            }

        }

        if(fenshenskill&&zibaoskill){
            return fenshenskill;
        }

        return null;
    },

    isMove:function(){
        return skillConf
        &&skillConf.skilltype=="move";
    },

    isFenshen:function(skillConf){
        return skillConf
                &&skillConf.skilltype=="fenshen"
                &&skillConf.npc=="self";
    },

    isZibao:function(skillConf){
        return skillConf
            &&skillConf.skilltype=="zibao";
    },

    isShanji:function(skillConf){
        return skillConf
            &&skillConf.skilltype=="moveAttack";
    },

    isBuff:function(skillConf){
        return skillConf
            &&skillConf.skilltype=="buff"
            &&!skillConf.single
            &&!skillConf.distance;
    },

    //随机一个主动技能
    getCanUseSkill:function(){

        //中了嘲讽不会施法技能
        if(_STATUSHELPERCHESS.getChaofenRole()!=null){
            return null;
        }

        if(_STATUSHELPERCHESS.hasStatus(_CHESSFIGHT.currentPlayer,'luan')){
            return null;
        }

        
        if(_CHESSFIGHT.currentPlayer.skill
            &&_CHESSFIGHT.currentPlayer.skill.length>0){
            var tempSkill = [];
            for(var i=0;i<_CHESSFIGHT.currentPlayer.skill.length;i++){
                var tempSkillConf = _SKILL[_CHESSFIGHT.currentPlayer.skill[i]];
                if(tempSkillConf
                    &&tempSkillConf.cut<=_CHESSFIGHT.currentPlayer.mg
                    &&tempSkillConf.skilltype!='getRole'
                ){
                    //if(tempSkillConf.skilltype=="move"){
                    //    if(_MCFW.isnearArmy(_CHESSFIGHT.currentPlayer)){
                    //        tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                    //    }
                    //}else 
                    if(tempSkillConf.skilltype=="biansen"){
                        if(!_CHESSFIGHT.currentPlayer.historyAnimals
                            ||!_CHESSFIGHT.currentPlayer.historyAnimals[tempSkillConf.animals]){
                            tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                        }
                    }
                    //else if(tempSkillConf.skilltype=="fenshen"){
                    //    var enablePosArr = _MCFW.getPosOfTargetForJump(_CHESSFIGHT.currentPlayer.chessPos.x,
                    //                                                    _CHESSFIGHT.currentPlayer.chessPos.y);
                    //    if(enablePosArr&&enablePosArr.length>0){
                    //        tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                    //    }
                    //}
                    else if(_MCA.isBuff(tempSkillConf)){

                        if(
                            tempSkillConf.status
                            &&!_STATUSHELPERCHESS.hasStatus(_CHESSFIGHT.currentPlayer,tempSkillConf.status)
                            &&!_CHESSFIGHT.currentRoleData.clearBody
                        ){
                            tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                        }

                    }else if(tempSkillConf.skilltype=='anqi'){
                        if(_MCAC.getRandomItem(_CHESSFIGHT.currentPlayer.isplayer)){
                            tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                        }
                    }else{
                        tempSkill.push(_CHESSFIGHT.currentPlayer.skill[i]);
                    }
                }
            }
            if(tempSkill.length>0){
                //console.log('可以使用的技能',tempSkill);
                var speskill = _MCA.speSkillUse(tempSkill);
                if(speskill){
                    return speskill;
                }

                var skill = _CU.randomObj(tempSkill);
                return skill;
            }
        }

        return null;

    },


    //获取当前行动角色的敌对角色集合
    getArmyArr:function(){

        //return _CHESSFIGHT.currentPlayer.isplayer?
        //       window.fightData.armyTeam:window.fightData.playerTeam;

        //判断是否中了嘲讽
        var chaofenRole = _STATUSHELPERCHESS.getChaofenRole();
        if(chaofenRole!=null&&chaofenRole.hp>0){
            return [chaofenRole];
        }

        var result = [];

        var ishunluan = _STATUSHELPERCHESS.hasStatus(_CHESSFIGHT.currentPlayer,'luan');

        for(var key in _CHESSFIGHT.playerNodeMap){
            var temprole = _CHESSFIGHT.playerNodeMap[key].info;

            if(!ishunluan){
                if(
                    temprole.isplayer!=_CHESSFIGHT.currentPlayer.isplayer
                    &&!_STATUSHELPERCHESS.hasStatus(temprole,"yin")
                ){
                    result.push(temprole);
                }
            }else{

                if(
                    temprole.isplayer==_CHESSFIGHT.currentPlayer.isplayer
                    &&!_STATUSHELPERCHESS.hasStatus(temprole,"yin")
                    &&!_CU.isSamePlayer(temprole,_CHESSFIGHT.currentPlayer)
                ){
                    result.push(temprole);
                }

            }

        }
        return result;
    },

    getArmyObjArr:function(){

        //return _CHESSFIGHT.currentPlayer.isplayer?
        //       window.fightData.armyTeam:window.fightData.playerTeam;
        var result = [];
        for(var key in _CHESSFIGHT.playerNodeMap){
            var temprole = _CHESSFIGHT.playerNodeMap[key].info;
            if(
                temprole.isplayer!=_CHESSFIGHT.currentPlayer.isplayer
                &&!_STATUSHELPERCHESS.hasStatus(temprole,"yin")
            ){
                result.push(_CHESSFIGHT.playerNodeMap[key]);
            }

        }
        return result;
    },

    //获取当前行动角色的敌对角色集合
    getFriendArr:function(){

        //return _CHESSFIGHT.currentPlayer.isplayer?
        //        window.fightData.playerTeam:window.fightData.armyTeam;
        var result = [];
        for(var key in _CHESSFIGHT.playerNodeMap){
            var temprole = _CHESSFIGHT.playerNodeMap[key].info;
            if(temprole.isplayer==_CHESSFIGHT.currentPlayer.isplayer){
                result.push(temprole);
            }

        }
        return result;

    },

    getFriendObjArr:function(){

        //return _CHESSFIGHT.currentPlayer.isplayer?
        //        window.fightData.playerTeam:window.fightData.armyTeam;
        var result = [];
        for(var key in _CHESSFIGHT.playerNodeMap){
            var temprole = _CHESSFIGHT.playerNodeMap[key].info;
            if(temprole.isplayer==_CHESSFIGHT.currentPlayer.isplayer){
                result.push(_CHESSFIGHT.playerNodeMap[key]);
            }

        }
        return result;

    },

    tryMoveSkill:function(skillName){
        //var skilldis = _CHESSFIGHT.currentRoleData.scope+skillConf.distance;
        var distance = _SKILL[skillName].distance+_CHESSFIGHT.currentRoleData.scope;
        var selfx = _CHESSFIGHT.currentPlayer.chessPos.x;
        var selfy = _CHESSFIGHT.currentPlayer.chessPos.y;
        
        //console.log('传送范围:',tempfanwei);
        var moveTarget = null;
        var jumpTarget = null;
        if(
            _CHESSFIGHT
            //&&_CHESSFIGHT.jumpMap
            &&!_CU.isObjEmpty(_CHESSFIGHT.jumpMap)
            &&Math.random()<(_MCA._JUMPPER+_CHESSFIGHT.currentRoleData.magicPer/100)
        ){
            moveTarget = _MCFW.getMoveTargetForJumpOut();
            if(moveTarget){
                var tempfanwei = _MCFW.getAllEmptyPos(false,distance,moveTarget.x,moveTarget.y);
                jumpTarget = _MCFW.getTargetForJumpOut(tempfanwei);
            }
        }else{
            var tempfanwei = _MCFW.getAllEmptyPos(false,distance,selfx,selfy);
            jumpTarget = _MCFW.getTargetForMoveAgain(tempfanwei,_CHESSFIGHT.playerNodeMap,_CHESSFIGHT.currentPlayer);
        }
        if(jumpTarget){
            //console.log('传送点:',moveTarget);
            _CHESSFIGHT.moveSkillAction(jumpTarget,moveTarget,skillName);
            return true;
        }else{
            return false;
        }
    },

    tryMoveAttackSkill:function(skillName){

        
        var sourceArr = _MCA.getArmyObjArr();

        //给敌方根据血量排序
        sourceArr.sort((a,b)=>{
            return a.info.hp-b.info.hp;
        });

        for(var i=0;i<sourceArr.length;i++){
            if(sourceArr[i].info.hp>0){

                if(_CHESSFIGHT.moveAttackSkillAction(sourceArr[i],skillName)){
                    return true;
                }

            }
        }
        

        return false;

    },

    tryMoveRoleSkill:function(skillName){
        var sourceArr = _MCA.getFriendObjArr();

        //给敌方根据血量排序
        sourceArr.sort((a,b)=>{
            return a.info.hp-b.info.hp;
        });

        for(var i=0;i<sourceArr.length;i++){
            if(sourceArr[i].info.hp>0
                &&!_CU.isPosNear(sourceArr[i].info.chessPos,_CHESSFIGHT.currentPlayer.chessPos)
            ){

                if(_CHESSFIGHT.moveRoleSkillAction(sourceArr[i],skillName)){
                    return true;
                }

            }
        }
        

        return false;
    },

    //选择能直接释放技能的目标
    tryTargetSkill:function(skillName){
        //var ishp = (_SKILL[skillName].skilltype=="buff");
        var skillConf = _SKILL[skillName];
        var isEnergy = skillConf.isEnergy;
        var ishp = (
            skillConf.skilltype=="buff"
            &&(skillConf.bhp||skillConf.php)
            &&!skillConf.status
            );//是否纯恢复技能

        var sourceArr = _SKILL[skillName].target=="army"?
                        _MCA.getArmyArr()
                        :_MCA.getFriendArr();

        //给敌方根据血量排序
        sourceArr.sort((a,b)=>{
            return a.hp-b.hp;
        });

        //先判定是否有能攻击到的目标 优先选择血量少的
        for(var i=0;i<sourceArr.length;i++){
            if(
                sourceArr[i].hp>0
                &&(
                    !ishp
                    ||(sourceArr[i].hp<sourceArr[i].maxhpForShow)
                )
                &&(
                    !isEnergy
                    ||!_CU.isSamePlayer(sourceArr[i],_CHESSFIGHT.currentPlayer)
                )
            ){
                if(_CHESSFIGHT.skillTargetAction(sourceArr[i],skillName)){
                    return true;
                }
            }
        }

        return false;

    },

    //选择收益最大的点释放范围技能
    trySkill:function(skillName){
        
        var skillConf = _SKILL[skillName];
        var playerScope = _ROLEDATA.getRealAttr(_CHESSFIGHT.currentPlayer,'scope');
        var ishp = (
                    skillConf.skilltype=="buff"
                    &&(skillConf.bhp||skillConf.php)
                    &&!skillConf.status
                    );//是否纯恢复技能
        var isSelfBuff = (
                            skillConf.skilltype=="buff"
                            &&!skillConf.single
                            &&!skillConf.distance
                        );//是否自身buff技能
        var skilldis = skillConf.distance?skillConf.distance:0;
            skilldis+=playerScope;
        var sourceArr = skillConf.target=="army"?
                        _MCA.getArmyArr()
                        :_MCA.getFriendArr();

        console.log('随机技能:',skillName);

        var posarr = [];
        if(
            skillConf.skilltype=="biansen"
            //||skillConf.skilltype=="fenshen"
            ||isSelfBuff
        ){
            posarr.push(_CHESSFIGHT.currentPlayer.chessPos);
        }else if(skillConf.skilltype=="fenshen"){
           
            for(var key in _CHESSFIGHT.currentFanwei){
                if(
                    !_CHESSFIGHT.currentFanwei[key].isattack
                    &&!_CHESSFIGHT.currentFanwei[key].ispass
                ){
                    var fwpos = _CHESSFIGHT.currentFanwei[key].pos;
                    var fwposx = fwpos[0];
                    var fwposy = fwpos[1];
                    var posObj = {x:fwposx,y:fwposy,nmb:0};
                    var emptyarr =_MCFW.getPosOfTargetForJump(posObj.x,posObj.y);
                    posObj.nmb = emptyarr?emptyarr.length:0;
                    if(posObj.nmb>0){
                        posarr.push(posObj);
                    }
                }
            }

        }else{
            for(var key in _CHESSFIGHT.currentFanwei){

                if(
                    !_CHESSFIGHT.currentFanwei[key].isattack
                    &&!_CHESSFIGHT.currentFanwei[key].ispass
                ){

                    var fwpos = _CHESSFIGHT.currentFanwei[key].pos;
                    var fwposx = fwpos[0];
                    var fwposy = fwpos[1];
                    var posObj = {x:fwposx,y:fwposy,nmb:0};
                    for(var i=0;i<sourceArr.length;i++){

                        if(
                            sourceArr[i].hp>0
                            &&(
                                !ishp
                                ||(sourceArr[i].hp<sourceArr[i].maxhpForShow)
                            )
                        ){

                            var playerPos = sourceArr[i].chessPos;
                            var playerDis = _CU.getPosDis(playerPos.x,playerPos.y,fwposx,fwposy);
                            //console.log('距离:',playerDis,skilldis);
                            if(playerDis<skilldis){
                                posObj.nmb++;
                            }

                        }

                    }

                    if(posObj.nmb>0)
                        posarr.push(posObj);

                }
            }
        }

        

        if(posarr.length>0){
            posarr.sort((a,b)=>{
                return b.nmb-a.nmb;
            });

            //console.log('范围技能点位:',skillName,posarr[0].nmb);

            _CHESSFIGHT.targetX = posarr[0].x;
            _CHESSFIGHT.targetY = posarr[0].y;
            _CHESSFIGHT.currentSkill = skillName;
            _CHESSFIGHT.skillActionByPos(_CHESSFIGHT.targetX,_CHESSFIGHT.targetY,_CHESSFIGHT.currentSkill);
            return true;
        }

        return false;

    },

    //直接选择能攻击的目标
    tryAttack:function(){

        var sourceArr = _MCA.getArmyArr();

        //给敌方根据血量排序
        sourceArr.sort((a,b)=>{
            return a.hp-b.hp;
        });

        //先判定是否有能攻击到的目标 优先选择血量少的
        for(var i=0;i<sourceArr.length;i++){
            if(sourceArr[i].hp>0){
                if(_CHESSFIGHT.attackTargetAction(sourceArr[i])){
                    return true;
                }
            }
        }

        return false;

    },

    //靠近血量最少的敌人
    tryMoveToArmy:function(){

        var armypath = _MCFW.loopFindArmy({
            x:_CHESSFIGHT.currentPlayer.chessPos.x,
            y:_CHESSFIGHT.currentPlayer.chessPos.y,
            flag:_CHESSFIGHT.currentPlayer.isplayer,
            player:_CHESSFIGHT.currentPlayer,
            tiledMap:_CHESSFIGHT.tiledMap,
            maxH:_CHESSFIGHT.maxH,
            maxW:_CHESSFIGHT.maxW,
            playerNodeMap:_CHESSFIGHT.playerNodeMap,
            chaofenRole:_STATUSHELPERCHESS.getChaofenRole()
        });

        /*
        console.log('寻找血量最少的敌人',
                    _CHESSFIGHT.currentPlayer,
                    _CHESSFIGHT.currentFanwei,
                    armypath?_CHESSFIGHT.currentFanwei[armypath.x+"-"+armypath.y]:null,
                    armypath?_CHESSFIGHT.playerNodeMap[armypath.x+"-"+armypath.y]:null,
                    armypath);
        */

        if(armypath){
           
            _CHESSFIGHT.targetX=armypath.x;
            _CHESSFIGHT.targetY=armypath.y;
            _CHESSFIGHT.waitAction();
            return true;
        }

        return false;
    },


    //原地待命
    waitBack:function(){

        _CHESSFIGHT.targetX=_CHESSFIGHT.currentPlayer.chessPos.x;
        _CHESSFIGHT.targetY=_CHESSFIGHT.currentPlayer.chessPos.y;
        _CHESSFIGHT.waitAction();

    },

    /*
    1.随机一个主动技能
    2.如果是指向性技能则寻找能直接释放的目标
    3.如果是非指向性技能则寻找收益最大的格子释放
    4.如果2,3寻找失败则进入普通攻击模式
    5.寻找能直接攻击的目标
    6.如果没有能直接攻击的目标则尝试靠近血量最少的敌人
    7.如果没有路径靠近血量最少的敌人则原地待机
    */
    aiAction:function(){

        var randomSkill = _MCA.getCanUseSkill();

        if(randomSkill&&_SKILL[randomSkill]){

            if(_SKILL[randomSkill].single){

                if(_SKILL[randomSkill].skilltype=="moveAttack"){

                    if(_MCA.tryMoveAttackSkill(randomSkill)){
                        return ;
                    }

                }else if(_SKILL[randomSkill].skilltype=="moveRole"){

                    if(_MCA.tryMoveRoleSkill(randomSkill)){
                        return ;
                    }

                }else if(_SKILL[randomSkill].skilltype=="move"){

                    if(_MCA.tryMoveSkill(randomSkill)){
                        return ;
                    }

                }else{

                    if(_MCA.tryTargetSkill(randomSkill)){
                        return ;
                    }

                }

            }else{

                if(_MCA.trySkill(randomSkill)){
                    return ;
                }

            }

        }

        //直接选择能攻击的目标
        if(_MCA.tryAttack()){
            return ;
        }

        //如果没有能直接攻击到的目标则选择距离最近的目标进行移动
        //如果中了混乱 则在原地不会向目标移动
        if(
            !_STATUSHELPERCHESS.hasStatus(_CHESSFIGHT.currentPlayer,'luan')
            &&_MCA.tryMoveToArmy()
        ){
            return ;
        }

        //如果没有则原地待机
       _MCA.waitBack();

    },


}